package com.swufestu.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyListActivity extends ListActivity implements Runnable {
    private static final String TAG = "MyListActivity";

    Handler handler;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_my_list);
//        ListView listView = findViewById(R.id.mylist);
        List<String> list1 = new ArrayList<String>();
        for (int i = 1; i < 100; i++) {
            list1.add("item" + i);
        }

//        ProgressBar progressBar = findViewById(R.id.progressBar);

//        String[] list_data = {"one", "two","three","four"};
//        ListAdapter adapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, list1);
//
//        setListAdapter(adapter);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.i(TAG, "handleMessage: ");
                if (msg.what == 9) {
                    ArrayList<String> rlist = (ArrayList<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(MyListActivity.this,
                            android.R.layout.simple_list_item_1, rlist);
                    setListAdapter(adapter);
                }
            }
        };

        //构造线程，并启动
//        RateThread rt = new RateThread(handler);
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {

        try{
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        Log.i(TAG,"run: AAAAA....");

        List<String> retlist = new ArrayList<String>();

        try {
//            Document doc = Jsoup.connect("http://www.usd-cny.com/bankofchina.htm").get();
            Document doc = Jsoup.connect("http://www.boc.cn/sourcedb/whpj/").get();
            //获取页面信息
            Log.i("xx", "run: title :" + doc.title());
            Elements tables = doc.getElementsByTag("tbody");
            //获取表格内容
            Element table1 = tables.get(1); //keypoint!
            Elements trs = table1.getElementsByTag("tr");
            trs.remove(0);
            //获取tr元素
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                String cname = tds.get(0).text();
                String cval = tds.get(5).text();
                retlist.add(cname + "===>>" + cval);
                Log.i("xx", "run: cname" + cname + "===>>" + "cval:" + cval);
            }
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        //获取MSG对象，发送消息给主线程
        Message msg = handler.obtainMessage(9,retlist);
        handler.sendMessage(msg);
        Log.i(TAG,"run: 消息已发送");
    }
}


////            Document doc = Jsoup.connect("http://www.usd-cny.com/bankofchina.htm").get();
//        Document doc = Jsoup.connect("http://www.boc.cn/sourcedb/whpj/").get();
//        //获取页面元素
//        Log.i(TAG,"run: title=" + doc.title());
//        //获取表格内容
//        Elements tables = doc.getElementsByTag("table");
//        Element table1 = tables.first();
////            Element table1 = tables.get(1);
//
//        //获取tr元素
//        Elements trs = table1.getElementsByTag("tr");
//        trs.remove(0);
//
//        for(Element tr : trs){
//        Elements tds = tr.getElementsByTag("td");
//        String cname = tds.get(0).text();
//        String cval = tds.get(5).text();
//        Log.i(TAG,"run: cname=" + cname + " ===> " + cval);
//        retlist.add(cname + " ===> " + cval);



//for(int i = 0;i<10;i++){
//        Document doc;
//        if (i==0){
//        doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/"+"index.html").get();
//        }else{
//        doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/"+"index_"+i+".html").get();
//        }
//        Log.i("xx", "run: title :"+doc.title());
//        Elements tables = doc.getElementsByTag("tbody");
//        Element table1 = tables.get(1);
//        Elements trs = table1.getElementsByTag("tr");
////                Elements tds = trs.first().getElementsByTag("td");
////                Log.i("run", "run: tds"+trs);
//        trs.remove(0);
//        //获取table内的tr
//        for(Element tr:trs){
//        Elements tds = tr.getElementsByTag("td");
//        String cname = tds.get(0).text();
//        String cval = tds.get(5).text();
//        retlist.add(cname+"===>>"+cval);
//        Log.i("xx", "run: cname"+cname+"===>>"+"cval:"+cval  );
//        }
//        }